var ns;
var app = new Vue({
    el: '#app',
    data: {
        roominfo: {
            RoomID: '',
            FullName: "",
            Name: "",
            RoomStateName: "",
            RoomStateID: 0,
            RelationName: "",
            RelatePhoneNumber: "",
            BuildingArea: 0,
            BuildOutArea: 0,
            RoomBasicRemark: '',//备注
        },
        ID: 0,
        statelist: [],
        staticData: true,
        dynamicData: false,
        isActive: false,
        relationlist: [],
        form: {},
        //classType 1-文本框 2-下拉框 3-时间框 4-labl文本 5-展示内容(只读) 6textarea
        list: [],
        tagList: [],
        show_cover: false,
        editIsShow:false,//后台返回的控制是否有修改权限字段
    },
    methods: {
        do_select_time: function (item) {
            var that = this;
            api.openPicker({
                type: 'date',
                date: that.form[item.id],
                title: '选择日期'
            }, function (ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month + '-' + day;
                    that.form[item.id] = currentTime;
                }
            });
        },
        edit: function () {
            var that = this;
            that.isActive = true;
            that.dynamicData = true;
            that.staticData = false;

        },
        save: function () {
            var that = this;

            var sourseValue = "";
            var IsSourse = false;//是否存在资源编号，存在就是必填项
            //自定义属性
            if (that.list.length > 0) {
                for (var i = 0; i < that.list.length; i++) {
                    that.list[i].value = that.form[that.list[i].id];
                    if (that.list[i].id == "Name") {
                        IsSourse = true;
                        sourseValue = that.list[i].value;
                    }
                }
            }

            if (sourseValue == '' && IsSourse) {
                api.toast({
                    msg: '请填写资源编号',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var options = {
                action: 'savedefinedfieldroomdata',
                RoomID: that.ID,
                fieldList: JSON.stringify(that.list),
            };
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '保存成功',
                        duration: 500,
                        location: 'bottom'
                    });
                    that.isActive = false;
                    that.dynamicData = false;
                    that.staticData = true;
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 10000,
                        location: 'bottom'
                    });
                }
            });
        },
        tel_phone: function (phonenumber) {
            api.call({
                type: 'tel_prompt',
                number: phonenumber
            });
        },
        get_relationdata: function (data, op) {
            var that = this;
            ns.post({
                RoomID: that.ID,
                action: 'getrelationrooms'
            }, function (succeed, data, err) {
                if (succeed) {
                    that.relationlist = data.list
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },
        get_data: function (data, op) {
            var that = this;
            ns.post({
                RoomID: that.ID,
                action: 'getdefinedfieldroomdata'
            }, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    that.list = data.list;
                    if (that.list.length > 0) {
                        //创建obj对象
                        var obj = new Object();
                        for (var i = 0; i < that.list.length; i++) {
                            obj[that.list[i].id] = that.list[i].value;
                        }
                        that.form = obj;
                    }
                    that.editIsShow=false;
                    if(data.CanEditRoomSource){
                        that.editIsShow=data.CanEditRoomSource;
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        do_select_tag: function(item) {
            item.isSelected = !item.isSelected;
        },
        do_edit_tag: function () {
            var that = this;
            that.show_cover = true;
            ns.post({
                RoomID: that.ID,
                action: 'gettaglistbyroomid'
            }, function (succeed, data, err) {
                if (succeed) {
                    that.tagList = data;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        saveTag: function () {
            var that = this;
            that.show_cover = true;
            var tagIdList = [];
            that.tagList.forEach(function (item) {
                if (item.isSelected) {
                    tagIdList.push(item.id);
                }
            })
            ns.post({
                RoomID: that.ID,
                tagIds: JSON.stringify(tagIdList),
                action: 'savetagbyroom'
            }, function (succeed, data, err) {
                if (succeed) {
                    that.show_cover = false;
                    api.toast({
                        msg: '保存成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();

                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        do_close_tag: function(){
            var that = this;
            that.show_cover = false;
        }
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    if (api.pageParam.id) {
        app.ID = api.pageParam.id;
        app.get_data();
        app.get_relationdata();//关联资源
    }

}
